package com.xdja.jwt.imagecollect.app.utils

import android.os.Bundle
import com.chad.library.adapter.base.diff.BaseQuickDiffCallback
import com.xdja.jwt.imagecollect.app.db.entity.UploadFileBean

/**
 * @author yuanwanli
 * @des
 * @date 2019/10/30
 */
class DiffUploadCallBack(newList: List<UploadFileBean>) :
    BaseQuickDiffCallback<UploadFileBean>(newList) {
    companion object {
        val hasStart: String = "hasStart"
        val hasError: String = "hasError"
        val uploadSize: String = "uploadSize"
    }
    override fun areItemsTheSame(oldItem: UploadFileBean, newItem: UploadFileBean): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: UploadFileBean, newItem: UploadFileBean): Boolean {
        //上传状态,上传进度,是否暂停,出错四种状态一致,认为不需要刷新
        return when {
            oldItem.state != newItem.state -> false
            oldItem.hasStart != newItem.hasStart -> false
            oldItem.hasError != newItem.hasError -> false
            (newItem.uploadSize * 100 / newItem.totalSize) != (oldItem.uploadSize * 100 / oldItem.totalSize) -> false
            else -> true
        }
    }

    override fun getChangePayload(oldItem: UploadFileBean, newItem: UploadFileBean): Any? {
        var bundle = Bundle()
        if (oldItem.hasStart != newItem.hasStart) {
            bundle.putBoolean(hasStart, newItem.hasStart)
        }
        if (oldItem.hasError != newItem.hasError) {
            bundle.putBoolean(hasError, newItem.hasError)
        }
        if (oldItem.uploadSize != newItem.uploadSize) {
            bundle.putLong(uploadSize, newItem.uploadSize)
        }
        return bundle
    }
}












